package com.example.base.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.base.R;
import com.example.base.bean.NavThree;
import com.example.base.bean.NavTwo;
import com.example.base.view.SysViewLayout;
import java.util.ArrayList;
import java.util.List;

public  class FlowAdapter2 extends BaseAdapter {
    private final List<NavTwo> dataList=new ArrayList<>();
    private final Context context;
    private final String TAG = "FlowAdapter2";

    public FlowAdapter2(Context context, List<NavTwo> dataList) {
        this.dataList.addAll(dataList);
        this.context = context;
    }
    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public java.lang.Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView==null){
            myViewHolder= new MyViewHolder();
            LayoutInflater inflater=LayoutInflater.from(context);
            convertView=inflater.inflate(R.layout.list_view_sys,null,true);
            myViewHolder.tv_title=convertView.findViewById(R.id.tv_sys_title);
            myViewHolder.flowLayout=convertView.findViewById(R.id.ViewLayout);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        NavTwo navTwo=dataList.get(position);
        myViewHolder.tv_title.setText(navTwo.getName());
        initFlow(navTwo.getArticles(),myViewHolder.flowLayout);
        return convertView;
    }
    private void initFlow(List<NavThree> articles, SysViewLayout sysViewLayout) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 5, 10, 5);
        if (sysViewLayout == null) {
            Log.d(TAG,"sysViewLayout is null so return");
            return;
        }
        sysViewLayout.removeAllViews();
        for (int i = 0; i < articles.size(); i++) {
            TextView tv = new TextView(context);
            tv.setPadding(28, 10, 28, 10);
            tv.setText(articles.get(i).getTitle());
            tv.setMaxEms(10);
            tv.setSingleLine();
            tv.setBackgroundResource(R.drawable.shape_textback);
            tv.setLayoutParams(layoutParams);
            sysViewLayout.addView(tv, layoutParams);
            tv.setOnClickListener(v -> Toast.makeText(context,"123",Toast.LENGTH_SHORT).show());
        }
    }

    public void setData(List<NavTwo> data) {
        if (null == data) {
            Log.d(TAG, "data is null so return");
            return;
        }
        Log.d(TAG, "data ==" + data);
        dataList.clear();
        dataList.addAll(data);
    }


    static class MyViewHolder {
        TextView tv_title;
        SysViewLayout flowLayout;
    }
}
